package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.BaseManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags="平台属性列表")
@RestController
@RequestMapping("/admin/product")
public class BaseManageController {

    @Autowired
    private BaseManageService baseManageService;

    /**
     * '/admin/product/deleteAttrInfoById/' + attrId
     * Get
     */
    @GetMapping("/deleteAttrInfoById/{attrId}")
    @ApiOperation("根据attrId软删除属性")
    public Result deleteAttrInfoById(@PathVariable("attrId") Long attrId){
        if(attrId != null && attrId != 0){
            baseManageService.deleteAttrInfoById(attrId);
        }
        return Result.ok();

    }


    /**
     * /admin/product/getAttrValueList/{attrId}
     * 修改按钮传回attrId进行数据回显
     * 根据平台属性Id 获取到平台属性值集合
     * Get
     */
    @GetMapping("/getAttrValueList/{attrId}")
    @ApiOperation("修改按钮传回attrId进行数据回显")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId){
//        List<BaseAttrValue> attrValueList = baseManageService.getAttrValueList(attrId);
//        return  Result.ok(attrValueList);
        //考虑到代码的可重用性,这里不用上边的方法,用另一种方式,看代码体会上边与下边的区别
        BaseAttrInfo baseAttrInfo=baseManageService.getAttrInfo(attrId);
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        return Result.ok(attrValueList);
    }


    /**
     * /admin/product/saveAttrInfo
     * 保存/修改平台属性
     * Post
     */
    @PostMapping("/saveAttrInfo")
    @ApiOperation("保存/修改平台属性")
    public Result saveAttrInfo(
            @RequestBody BaseAttrInfo baseAttrInfo
    ){
        //前台数据都封装在baseAttrInfo中
        baseManageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    /**
     * /admin/product/attrInfoList/{category1Id}/{category2Id}/{category3Id}
     * 根据分类id获取平台属性集合
     * Get
     */
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    @ApiOperation("根据分类id获取平台属性集合")
    public Result attrInfoList(
            @PathVariable("category1Id") Long category1Id,
            @PathVariable("category2Id") Long category2Id,
            @PathVariable("category3Id") Long category3Id
    ){
        List<BaseAttrInfo> attrInfoList=baseManageService.attrInfoList(category1Id,category2Id,category3Id);
        return Result.ok(attrInfoList);
    }


    /**
     * /admin/product/getCategory3/{category2Id}
     * 获取三级分类数据
     * Get
     */
    @GetMapping("/getCategory3/{category2Id}")
    @ApiOperation("获取三级分类数据")
    public Result getCategory3(@PathVariable("category2Id") Long category2Id){
        List<BaseCategory3> category3List =baseManageService.getCategory3(category2Id);
        return Result.ok(category3List);
    }


    /**
     * /admin/product/getCategory2/{category1Id}
     * 获取二级分类数据
     * Get
     */
    @GetMapping("/getCategory2/{category1Id}")
    @ApiOperation("获取二级分类数据")
    public Result getCategory2(@PathVariable("category1Id") Long category1Id){
        List<BaseCategory2> category2List=baseManageService.getCategory2(category1Id);
        return Result.ok(category2List);
    }


    /**
     * /admin/product/getCategory1
     * 获取一级分类数据
     * Get
     */
    @GetMapping("/getCategory1")
    @ApiOperation("获取一级分类数据")
    public Result getCategory1(){
        List<BaseCategory1>category1List= baseManageService.getCategory1();
        return Result.ok(category1List);
    }
}
